--[[ 
Title: Suicide Kings
Author: Brom
Version: 0.1
]]

function SuicideKings:GetUIDFromName( name )

    for uid,v in pairs( self.db.profile.roster ) do     
        if ( v["main"] == name ) then
            return uid
        end
    end
    
    SuicideKings:Print( "Warning: could not find UID for " .. name )
    return 0
    
end

function SuicideKings:ImportFromKSK()

    SuicideKings:Print( "-- Importing KonferSK Data --" )
    
    --[[
    import rosters 
    ]]--
        
    SuicideKings:Print( "Importing KonferSK roster..." )
    self.db.profile.roster = {}
            
    -- search for unique mains
    for _,v in pairs( ksk.sortedusers ) do
        if ( ksk:UserIsAlt(v.id) == false ) then
        
            -- generate unique key
            local uid = SuicideKings:UID( self.db.profile.roster )
            
            -- associate with main character key
            self.db.profile.roster[uid] = {};
            self.db.profile.roster[uid]["main"] = ksk.users[v.id].name;
                        
            -- debug
            -- SuicideKings:Print( "Imported main: " .. ksk.users[v.id].name .. " (key = " .. uid .. ")" )
            
        end
    end
    
    -- search for alts
    for _,v in pairs( ksk.sortedusers ) do
        if ( ksk:UserIsAlt(v.id) == true ) then
    
            -- ksk data
            local altName = ksk.users[v.id].name;
            local mainIdx = ksk.users[v.id].main;
            local mainName = ksk.users[mainIdx].name;
            
            -- find a main in our data with that name
            local found = false
            for uid,vv in pairs( self.db.profile.roster ) do        
                 if ( vv["main"] == mainName ) then
                    found = true
                    
                    -- check for alts table
                    if ( not self.db.profile.roster[uid]["alts"] ) then
                        self.db.profile.roster[uid]["alts"] = {}
                    end
                        
                    -- associate alt name with character key
                    table.insert( self.db.profile.roster[uid]["alts"], altName )
                    
                    -- debug
                    -- SuicideKings:Print( "Imported alt: " .. altName .. " (main = " .. vv["main"] .. " | key = "  .. uid .. ")" )
                end
            end 
           
            -- debugging
            if ( found == false ) then
                SuicideKings:Print( "Warning: could not find main for " .. altName )
            end
            
        end
    end
    
    --[[
    import lists 
    ]]--
        
    SuicideKings:Print( "Importing KonferSK lists..." )
    self.db.profile.lists = {}
    
    -- get lists
    for _,v in pairs( ksk.sortedlists ) do
    
        -- generate unique key
        local uid = SuicideKings:UID( self.db.profile.lists )
    
        -- ksk data
        local listName = ksk.lists[v.id].name
        local listUsers = ksk.lists[v.id].users
        
        -- associate with main character key
        self.db.profile.lists[uid] = {};
        self.db.profile.lists[uid]["name"] = listName;
        
        -- get user ids
        self.db.profile.lists[uid]["users"] = {}
        for _,vv in pairs( listUsers ) do
            local userName = ksk.users[vv].name;
            local userId = SuicideKings:GetUIDFromName( userName )
            table.insert( self.db.profile.lists[uid]["users"], userId )
        end
        
        SuicideKings:Print( "Imported list: " .. listName )
    
    end
    
end
